// pages/goods/goodlist/index.js
var app = getApp();
var core = app.requirejs('core');
var $ = app.requirejs('jquery');
var diyad = app.requirejs('biz/ad');
const api = require("../../../utils/api.js")
Page({

  /**
   * 页面的初始数据
   */
  data: {
    current: 1,
    loadKey: true,
    list: []
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    this.setData({
      id: options.id
    })
    this.getList()
    wx.setNavigationBarTitle({
      title: options.title,
    })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function() {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
    let _this = this
    if (this.data.loaded == true) {
      return
    }
    _this.getList();
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  },
  //获取商品列表
  getList: function() {
    var $this = this;
    let apicontent = api.goods.goodsListByCate()
    if ($this.data.loadKey) {
      $this.setData({
        loadKey: false
      })
      var keywords = $this.data.navname
      core.get(apicontent, {
        current: $this.data.current,
        id: $this.data.id
      }, function(result) {
        var data = {
          loading: false,
          count: result.data.total,
          show: true,
          loadKey: true
        };

        if (result.data.rows.length == 0) {
          result.data.rows = [];
        }
        if (result.data.rows.length > 0) {
          if (result.data.rows.option) {
            // result.data.rows.option.banner = JSON.stringify(result.data.rows.option.banner)
          }
          data.current = $this.data.current + 1;
          data.list = $this.data.list.concat(result.data.rows);
          if (result.data.rows.length < result.data.size) {
            data.loaded = true
          }
          result.data.rows.forEach(e => {
            e.imgBase = ""
          })
        }

        $this.setData(data);
      });
    }
  },
})